package com.zhyl.demo.controller;

import com.zhyl.demo.entity.Doctor;
import com.zhyl.demo.entity.User;
import com.zhyl.demo.service.DoctorService;
import com.zhyl.demo.service.UserService;
import com.zhyl.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.View;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yang3
 */
@RestController
@RequestMapping("/doctor")
public class DoctorController {

    private final DoctorService doctorService;
    private final UserService userService;

    @Autowired
    private DoctorController(DoctorService doctorService,
                             UserService userService, View error){
        this.doctorService = doctorService;
        this.userService = userService;
    }

    @RequestMapping("/docInfo")
    public Result docInfo(Integer docId){
        Doctor doctor = doctorService.docInfo(docId);
        if (doctor!=null) {
            return Result.success(doctor);
        }else {
            return Result.error("查无此人");
        }
    }

    @RequestMapping("/getDoc")
    public Result getDoc(Integer userDoc)
    {
        if (userDoc==null){
            return Result.error("-1","信息有误");
        }
        Doctor doc = doctorService.getDoc(userDoc);
        if (doc!=null) {
            return Result.success(doc);
        }else {
            return Result.error("查无此人");
        }
    }

    @RequestMapping("/getAllDoc")
    public Result getAllDoc(){
        ArrayList<Doctor> allDoc = doctorService.getAllDoc();
        if (allDoc!=null){
            return Result.success(allDoc);
        }else {
            return Result.success(null);
        }
    }

    @RequestMapping("/docList")
    public Result docList(String specialty)
    {
        ArrayList<Doctor> doctors = doctorService.docList(specialty);
        if (doctors!=null){
            return Result.success(doctors);
        }else {
            return Result.error("暂无数据");
        }

    }

    @RequestMapping("/searchDocByName")
    public Result docList(String specialty,String docName)
    {
        if (specialty==null || docName==null){
            return Result.error("-1","信息有误");
        }
        ArrayList<Doctor> doctors = doctorService.searchDocByName(specialty,docName);
        if (doctors!=null){
            return Result.success(doctors);
        }else {
            return Result.error("暂无数据");
        }

    }

    @RequestMapping("/selectBySpecialty")
    public Result selectBySpecialty(String sname)
    {
        if (sname==null){
            return Result.error("-1","信息有误");
        }
        ArrayList<Doctor> doctors = doctorService.selectBySpecialty(sname);
        if (doctors!=null){
            return Result.success(doctors);
        }else {
            return Result.error("暂无数据");
        }
    }

    @RequestMapping("/updateDocInfo")
    public Result updateDocInfo(@RequestBody Doctor doctor){
        if (doctor==null || doctor.getId()<=0){
            return Result.error("-1","信息有误");
        }

        boolean b = doctorService.updateDocInfo(doctor);

        if (b){
            return Result.success(doctor);
        }else {
            return Result.error("修改失败");
        }
    }

    @RequestMapping("/deleteDoc")
    public Result deleteDoc(Integer id){

        if (id==null || id<=0){
            return Result.error("-1","信息有误");
        }
        Doctor doctor = doctorService.docInfo(id);
        boolean b = doctorService.deleteDoc(id);
        if (b){
            Boolean b1 = userService.deleteUser(doctor.getUserDoc());
            if (b1){
                return Result.success(null);
            }else {
                return Result.error("删除失败");
            }
        }else {
            return Result.error("删除失败");
        }
    }

    @RequestMapping("/addDoc")
    public Result addDoc(@RequestBody Doctor doctor) {
        if (doctor==null)
        {
            return Result.error("-1","信息有误");
        }
        User user = new User();
        user.setUsername(String.valueOf(userService.getRandomNumber()));
        user.setPassword(String.valueOf(userService.getRandomNumber()));
        user.setRole("doctor");
        Boolean register = userService.register(user);
        if (register){
            doctor.setUserDoc(user.getId());
            Boolean b = doctorService.addDoc(doctor);
            if (b){
                return Result.success(doctor);
            }else {
                return Result.error("添加失败");
            }
        }else {
            return Result.error("添加失败");
        }

    }

    @RequestMapping("/getDocByName")
    public Result getDocByName(String name){
        if (name==null || name.equals(""))
        {
            return Result.error("-1","信息有误");
        }
        List<Doctor> doctors = doctorService.getDocByName(name);
        if (doctors!=null){
            return Result.success(doctors);
        }else {
            return Result.error("暂无数据");
        }
    }




}
